#include "sparepart.h"
#include "ui_sparepart.h"
#include "entity/sparepartentity.h"
#include "dbmanager.h"
#include <QMessageBox>

SparePart::SparePart(QWidget *parent) :
    QWidget(parent), ui(new Ui::SparePart)
{
    ui->setupUi(this);
    connect(ui->okButton, SIGNAL(clicked()), this, SLOT(slotOkClicked()));
    connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(slotClearClicked()));
}

SparePart::~SparePart()
{
    delete ui;
}

void SparePart::slotOkClicked()
{
    SparePartEntity * sp = new SparePartEntity();
    sp->setSpCode(ui->spCodeText->text());
    sp->setSpName(ui->spNameText->text());

    bool ok = false;
    float unitCost = ui->unitCostText->text().toFloat(&ok);
    if (ok)
        sp->setUnitCost(unitCost);
    else
        qDebug() << "unit cost error"; //QMessageBox::Critical(this, "Error", "Unit Cost is wrong. Please enter correct value");

    ok = false;
    float minCost = ui->minCostText->text().toFloat(&ok);
    if (ok)
        sp->setMinCost(minCost);
    else
        qDebug() << "min cost error"; //QMessageBox::Critical(this, "Error", "Min Cost is wrong. Please enter correct value");

    ok = false;
    float maxCost = ui->maxCostText->text().toFloat(&ok);
    if (ok)
        sp->setMaxCost(maxCost);
    else
        qDebug() << "max cost error"; //QMessageBox::Critical(this, "Error", "Max Cost is wrong. Please enter correct value");

    sp->setDesc(ui->descText->text());
    sp->setDeleted(0);

    DBManager * dbm = DBManager::getGlobal();
    dbm->insertSparePart(sp);
    delete sp;
}

void SparePart::slotClearClicked()
{

}
